import requests
from base_tools.my_logger import logger
import traceback
from config import *

class BaseAPI():

    def __init__(self):
        self.params = None

    # def my_request(self, method, url, **kwargs):
    #     """
    #     接口请求的封装
    #     :param method:
    #     :param url:
    #     :param kwargs:
    #     :return:
    #     """
    #     logger.info("开始请求接口，请求方式：{}，请求url：{}, 请求参数：{}".format(method, url, kwargs))
    #     try:
    #         if "params" in kwargs.keys():
    #             if self.params:
    #                 kwargs["params"]["access_token"] = self.token
    #                 self.rep = requests.request(method=method, url=url, **kwargs)
    #
    #             else:
    #                 self.rep = requests.request(method=method, url=url, **kwargs)
    #         else:
    #             self.rep = requests.request(method=method, url=url, params=self.params, **kwargs)
    #         return self.rep.json()
    #     except:
    #         logger.warning("接口请求失败，报错信息：", traceback.print_exc())
    #         raise Exception("接口请求失败，请检查请求参数信息。。。")

    def my_request(self, method, url, **kwargs):
        """
        接口请求的封装
        :param method:
        :param url:
        :param kwargs:
        :return:
        """
        logger.info("开始请求接口，请求方式：{}，请求url：{}, 请求参数：{}".format(method, url, kwargs))
        try:
            self.rep = requests.request(method=method, url=url, **kwargs)
            return self.rep.json()
        except Exception as e:
            logger.warning("接口请求失败....")
            raise Exception("接口请求失败，请检查请求参数信息。。。")

    def get_mail_list_manager_token(self):
        logger.info("获取token。。。。。")
        self.my_request(method="get", url=token_url, params=token_params)
        self.token = self.rep.json().get("access_token")
        logger.info("token值为：{}".format(self.token))
        self.params = {
            "access_token": self.token
        }